package com.example.myblog.config;


import com.example.myblog.common.GloableVariable;
import com.example.myblog.entity.UserInfo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;

/*
    登录拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute(GloableVariable.USER_SESSION_KEY) != null) {
            System.out.println("当前登录用户：" +
                    ((UserInfo) session.getAttribute(GloableVariable.USER_SESSION_KEY)).getUsername());
            return true;//已经登录
        }
        response.sendRedirect("/login.html");//转到登录界面
        return false;
    }
}
